package com.enoram.gwt.desktop.client.ui;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class LoginDialog extends DialogBox{

	private Panel panel = new VerticalPanel();
	
	private TextBox userText = new TextBox();
	private PasswordTextBox passText = new PasswordTextBox();
	
	private Grid grid = new Grid(4,2);
	
	public LoginDialog(){
		
		setWidget(panel);
		
		setAnimationEnabled(true);
		setGlassEnabled(true);
		
		grid.setText(1,0, "UserName:");
		grid.setWidget(1,1, userText);
		
		grid.setText(2,0, "Password:");
		grid.setWidget(2,1, passText);
		
		panel.add(grid);
		
		
		setText("Login to your account");
		
		panel.add(new Button("Login", new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				
				hide(true);
				
			}
		}));
		
		
		
		center();
		
		show();
		
		userText.setFocus(true);
		
		handleKeyboard();
	}


	private void handleKeyboard(){
		userText.addKeyDownHandler(new KeyDownHandler() {
			
			@Override
			public void onKeyDown(KeyDownEvent event) {
				if(event.getNativeKeyCode() == KeyCodes.KEY_ENTER){
					hide();
				}
				
			}
		});
		
		passText.addKeyDownHandler(new KeyDownHandler() {
			
			@Override
			public void onKeyDown(KeyDownEvent event) {
				if(event.getNativeKeyCode() == KeyCodes.KEY_ENTER){
					hide();
				}
				
			}
		});		
	}
	
	public String getUserName(){
		return userText.getText();
	}
	
	public String getPassword(){
		return passText.getText();
	}
	
	
}
